package com.upb.webserver.datafetcher;

import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.rest.fisco.data.resp.ChainInfo;
import com.upb.webserver.rest.fisco.FiscoDataFetchRestService;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * data-fetcher merge interface service
 *
 */
@Log4j2
@Service
public class DataFetcherService {

    @Autowired
    private FiscoDataFetchRestService fiscoDataFetchRestService;

    /**
     * get chain list.
     */
    public BasePageResponse getChainList() {
        BasePageResponse restRsp = BasePageResponse.initialize();
        List<ChainInfo> chainList = new ArrayList<>();
        Pair<Integer, List<ChainInfo>> fiscoList = fiscoDataFetchRestService.getChainListPair();
        if (fiscoList.getKey() > 0) {
            chainList = fiscoList.getValue();
        }
        restRsp.setData(chainList);
        restRsp.setTotalCount(fiscoList.getKey());
        return restRsp;
    }

    /**
     * search data by keyword.
     */
    public BasePageResponse searchByKeyword(Integer pageNumber, Integer pageSize, String keyword) {
        BasePageResponse restRsp = BasePageResponse.initialize();
        try {
            restRsp = fiscoDataFetchRestService.searchByKeyword(pageNumber, pageSize, keyword);
            return restRsp;
        } catch (Exception e) {
            log.warn("data-fetcher searchByKeyword from fisco fail.");
        }
        return restRsp;
    }

}
